# Permutations of elements of an Array. Very usefull :)
# Don't remember where i got this but is not mine.
# This is used on rails_resources.rb to look for parent child relationships
# of resources whithout any complex modification of the rails resource creation mechanism.
class Array
  def perm(n = size)
    if size < n or n < 0
    elsif n == 0
      yield([])
    else
      self[1..-1].perm(n - 1) do |x|
	(0...n).each do |i|
	  yield(x[0...i] + [first] + x[i..-1])
	end
      end
      self[1..-1].perm(n) do |x|
	yield(x)
      end
    end
  end
end
